<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="课程类型" prop="courseTypeName">
        <el-input
          v-model="dataForm.courseTypeName"
          placeholder="课程类型"
        ></el-input>
      </el-form-item>


    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import course from './course.vue'
import th from 'element-ui/src/locale/lang/th'

export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: 0,
        courseTypeName: '',
        createTime: '',
        updateTime: '',
        idDelete: ''
      },
      courseTypeOption: [],
      dataRule: {
        courseTypeName: [
          { required: true, message: '课程类型名称不能为空', trigger: 'blur' }
        ],
        createTime: [
          { required: false, message: '创建时间不能为空', trigger: 'blur' }
        ],
        updateTime: [
          { required: false, message: '修改时间不能为空', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    init (id) {
      this.getCoureseTypeList()
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/course/coursetype/info/${this.dataForm.id}`
            ),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.courseTypeName = data.courseType.courseTypeName;
              this.dataForm.createTime = data.courseType.createTime;
              this.dataForm.updateTime = data.courseType.updateTime;
              this.dataForm.idDelete = data.courseType.idDelete;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/course/coursetype/${!this.dataForm.id ? 'save' : 'update'}`
            ),
            method: 'post',
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              courseTypeName: this.dataForm.courseTypeName,
              createTime: this.dataForm.createTime,
              updateTime: this.dataForm.updateTime,
              idDelete: this.dataForm.idDelete
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit('refreshDataList');
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    // 获取课程类型列表
    getCoureseTypeList () {
      this.$http({
        url: this.$http.adornUrl(
          '/course/coursetype/findCourseTypeList'
        ),
        method: 'get'
      }).then(({ data }) => {
        if (data && data.code === 0) {
          console.log(data.courseTypeList);
          this.courseTypeOption = data.courseTypeList;
        } else {
          this.courseTypeOption = [];
        }
      });
    }

  }
};
</script>
